package services

import (
	"fmt"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/db_live"
	"longmen/server/pkg/db/mysql/db_main"
	"longmen/server/pkg/db/mysql/models/models_live"
	"longmen/server/pkg/db/redis/cache"

	"github.com/jinzhu/copier"
	"longmen/server/pkg/common/api_models"
)

func (s *Service) GetHomeData() (*api_models.GetHomeDataResp, error) {
	fName := "GetHomeData"
	//主播
	user, err := db_main.GetUsersParam(map[string]interface{}{
		"is_anchor": 1,
	}, "rec_weight desc, anchor_point desc", 1, 8)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	userIds := make([]int, 0)
	for idx := range user {
		userIds = append(userIds, user[idx].Id)
	}
	anchorMap, err := s.GetAnchors(0, userIds, "", 0, 0)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	anchors := make([]*api_models.Anchor, 0)
	for key := range anchorMap {
		anchors = append(anchors, anchorMap[key])
	}
	anchorlives, err := db_live.GetLivesParam(map[string]interface{}{"anchorid": map[string]interface{}{"in": userIds}, "status": 1}, "", 1, len(userIds))
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	anchorliveMap := make(map[int]*models_live.Live)
	for idx := range anchorlives {
		anchorliveMap[anchorlives[idx].Anchorid] = anchorlives[idx]
	}

	resp := &api_models.GetHomeDataResp{}

	//分类
	categorys, err := db_live.GetLiveCategoryParam(map[string]interface{}{"status": 1}, "")
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	ids := make([]int, 0)
	for idx := range categorys {
		lives, err := db_live.GetLivesParam(map[string]interface{}{"liveid": map[string]interface{}{">": 0}, "categoryid": categorys[idx].Id, "status": 1}, "hot desc, start_stamp desc", 1, 15)
		if err != nil {
			continue
		}
		for i := range lives {
			ids = append(ids, lives[i].Anchorid)
		}
		tmp := &api_models.CategoryLive{}
		_ = copier.Copy(tmp, categorys[idx])
		_ = copier.Copy(&tmp.Lives, &lives)
		resp.Categorys = append(resp.Categorys, tmp)
	}
	anchorMap, err = s.GetAnchors(0, ids, "", 0, 0)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	for k := range resp.Categorys {
		for j := range resp.Categorys[k].Lives {
			if _, ok := anchorMap[resp.Categorys[k].Lives[j].Anchorid]; ok {
				resp.Categorys[k].Lives[j].Anchor = anchorMap[resp.Categorys[k].Lives[j].Anchorid]
			}
		}
	}

	resp.RecommendLives = make([]*models_live.Live, 0)
	resp.HotLives = make([]*models_live.Live, 0)
	resp.RecoomendAnchors = make([]*api_models.HomeAnchor, 0)

	return resp, nil
}

func (s *Service) GetNewHomeData() (*api_models.NewHomeDataResp, error) {
	fName := "GetNewHomeData"
	rsp, err := cache.GetNewHomeDataCache()
	if err == nil {
		return rsp, nil
	}
	sportLives, err := db_live.GetNewLivesParam(map[string]interface{}{"status": 1, "category": 2}, "heat desc, id desc", 1, 8)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	entertainmentLives, err := db_live.GetNewLivesParam(map[string]interface{}{"status": 1, "category": 1}, "heat desc, id desc", 1, 6)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}

	newHomeDataResp := new(api_models.NewHomeDataResp)
	newHomeDataResp.EntertainmentLives = entertainmentLives
	newHomeDataResp.SportLives = sportLives
	cache.SetNewHomeDataCache(newHomeDataResp)
	return newHomeDataResp, nil
}

func (s *Service) MGetHomeData(r *api_models.GetMHomeDataReq) (*api_models.GetMHomeDataResp, error) {
	fName := "MGetHomeData"
	lives_out := make([]*api_models.LiveAnchor, 0)
	hotlives_out := make([]*api_models.LiveAnchor, 0)
	recAnchors_out := make([]*api_models.HomeAnchor, 0)
	recmatchs_out := make([]*api_models.HomeAnchor, 0)
	lives, err := db_live.GetLivesParam(map[string]interface{}{"status": 1}, "id desc", r.Page, r.Size)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	tmpIds := make([]int, 0)
	for i := range lives {
		tmpIds = append(tmpIds, lives[i].Anchorid)
	}
	err = copier.Copy(&lives_out, &lives)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	anchorMap, err := s.GetAnchors(0, tmpIds, "", 0, 0)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	for i := range lives_out {
		if _, ok := anchorMap[lives_out[i].Anchorid]; ok {
			lives_out[i].Anchor = anchorMap[lives_out[i].Anchorid]
		}
	}

	if r.Page == 1 {
		hotlives, err := db_live.GetLivesParam(map[string]interface{}{"status": 1}, "heat desc, id desc", 1, 8)
		if err != nil {
			return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		}
		err = copier.Copy(&hotlives_out, &hotlives)
		if err != nil {
			return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		}

		recAnchors, err := db_main.GetUsersParam(map[string]interface{}{"is_anchor": 1}, "rec_weight desc,anchor_point desc,regist_time desc", 1, 5)
		if err != nil {
			return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		}
		tmpIds = make([]int, 0)
		for i := range recAnchors {
			tmpIds = append(tmpIds, recAnchors[i].Id)
		}
		tmpLives, err := db_live.GetLivesParam(map[string]interface{}{"anchorid": map[string]interface{}{"in": tmpIds}, "status": 1}, "", 0, 0)
		if err != nil {
			return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		}
		err = copier.Copy(&recAnchors_out, &recAnchors)
		if err != nil {
			return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		}
		for i := range recAnchors_out {
			for j := range tmpLives {
				if recAnchors_out[i].Id == tmpLives[j].Anchorid {
					recAnchors_out[i].Live = tmpLives[j]
					break
				}
			}
		}
	}

	resp := &api_models.GetMHomeDataResp{
		Hot:        hotlives_out,
		RecAnchors: recAnchors_out,
		Lives:      lives_out,
		RecMatchs:  recmatchs_out,
	}
	return resp, nil
}
